import moment from "moment";
import "moment/dist/locale/zh-cn";

moment.locale("zh-cn");

export const getFormatTime = (time: string | number, format?: string) => {
  format = format || "lll";
  // 由于后端时间是0区时间
  // const s = '2013-01-01T00:00:00';
  // return moment(time).utcOffset(s).format(format);
  return moment(time).format(format);
};

// cookie相关设置
export function getCookie(name: any) {
  const nameEQ = name + "=";
  const ca = document.cookie.split(";"); // 把cookie分割成组
  for (let i = 0; i < ca.length; i++) {
    let c = ca[i]; // 取得字符串
    while (c.charAt(0) == " ") {
      // 判断一下字符串有没有前导空格
      c = c.substring(1, c.length); // 有的话，从第二位开始取
    }
    if (c.indexOf(nameEQ) == 0) {
      // 如果含有我们要的name
      return unescape(c.substring(nameEQ.length, c.length)); // 解码并截取我们要值
    }
  }
  return false;
}
// 清除cookie
export function clearCookie(name: any) {
  setCookie(name, "", -1);
}
// 设置cookie
export function setCookie(name: any, value: any, seconds2: any) {
  const seconds = seconds2 || 0; //seconds有值就直接赋值，没有为0，这个根php不一样。
  let expires = "";
  if (seconds != 0) {
    //设置cookie生存时间
    const date: any = new Date();
    date.setTime(date.getTime() + seconds * 1000);
    expires = "; expires=" + date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expires + "; path=/"; //转码并赋值
}

export function getLang() {
  return getCookie("language")
    ? getCookie("language")
    : import.meta.env.VITE_DEFAULT_LANGUAGE;
}
